﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlackBird.Models;
using BlackBird.DataAccess;
using BlackBird.ControllerHelpers;
using BlackBird.Resources;

namespace BlackBird.Controllers
{
    public partial class FuKuanDanController : Controller
    {
        //
        // GET: /FuKuanDan/

        public virtual ActionResult Index()
        {
            return View();
        }

        //
        // GET: /FuKuanDan/Details/5

        public virtual ActionResult Details(string id)
        {
            var fukuandan = FuKuanDanDataAccess.FindFuKuanDan(Convert.ToInt32(id));
            UpdateViewDataAtRuntime(fukuandan); 
            return View(MVC.FuKuanDan.Views.FuKuanDan, fukuandan);
        }

        //
        // GET: /FuKuanDan/Create

        public virtual ActionResult Create(string ParentId)
        {
            var userDb = AccountHelper.GetCurrentUser(this);
            var projectDm = ProjectDataAccess.GetProject(ParentId);

            //当前项目已有的付款单数目
            int alreadyFuKuanDanNumber =
                FuKuanDanDataAccess.AllFuKuanDanDMForCurrentProject(Convert.ToInt32(ParentId)).Count;
            int bianhao = alreadyFuKuanDanNumber + 1;

            var fukuandan = new FuKuanDanModel()
            {
                ProjectId = ParentId,
                //默认值为工程名
                ProjectName = projectDm.BasicInfo.Name,
                CreateUser_idUser = userDb.idUser,
                TianBiaoRen = userDb.DisplayName,
                ShengQingRiQi = DateTime.Now.ToString("yyyy-MM-dd"),
                //1 => 001
                BianHao = String.Format("{0:000}", bianhao),
                Status = SupportTypeStatus.CaoGao
            };

            UpdateViewDataAtRuntime(fukuandan);

            return View(MVC.FuKuanDan.Views.FuKuanDan, fukuandan);
        } 

        [HttpPost]
        public virtual ActionResult ResolveFuKuanDan(FuKuanDanModel fukuandan)
        {
            try
            {
                UpdateViewDataAtRuntime(fukuandan);

                //get the button user click
                string clickButton = Request.Form["Action"];

                //Handle bijiadan acccording to the button user click
                if (clickButton == BBStrings.Create)
                {
                    GoToNextStep(fukuandan);
                    FuKuanDanDataAccess.AddFuKuanDan(fukuandan, this);
                }
                else if (clickButton == BBStrings.ShenHeResult_Pass)
                {
                    //当部门负责人为空的时候，填入名字， 说明这是第二部
                    //否则忽略掉
                    if (string.IsNullOrEmpty(fukuandan.BuMenFuZeRen))
                        fukuandan.BuMenFuZeRen = AccountHelper.GetCurrentUser(this).DisplayName;

                    GoToNextStep(fukuandan);
                    FuKuanDanDataAccess.UpdateFuKuanDan(fukuandan, this);
                }
                else if (clickButton == BBStrings.ShenHeResult_Fail)
                {
                    GoToPreviousStep(fukuandan);
                    FuKuanDanDataAccess.UpdateFuKuanDan(fukuandan, this);
                }
                else if (clickButton == BBStrings.GuiDang)
                {
                    fukuandan.Status = SupportTypeStatus.GuiDang;
                    FuKuanDanDataAccess.UpdateFuKuanDan(fukuandan, this);
                }
                else if (clickButton == BBStrings.ZuoFei)
                {
                    fukuandan.Status = SupportTypeStatus.ZuoFei;
                    FuKuanDanDataAccess.UpdateFuKuanDan(fukuandan, this);
                }
                else if (clickButton == BBStrings.Close)
                {
                    //nothing to do
                }

                return RedirectToAction(MVC.Home.Index());
            }
            catch(Exception ex)
            {
                return View();
            }
        }

        public void GoToNextStep(FuKuanDanModel dm)
        {
            //normally the status is ShengHeZhong when create fukuandan at the first time
            if (dm.Status == SupportTypeStatus.CaoGao)
                dm.Status = SupportTypeStatus.ShengHeZhong;

            //version 2 begin
            var currentUser = AccountHelper.GetCurrentUser(this);
            int maxStep = QuanXianAndProcessHelper.GetUserMaxStepNumber(currentUser, SupportTypes.FukuanDan);
            var potentialStepNumber = maxStep + 1;
            //version 2 end

            var isValid = QuanXianAndProcessHelper.IsStepNumberAvailable(SupportTypes.FukuanDan, potentialStepNumber);
            if (isValid)
                dm.CurrentStep = potentialStepNumber;
            else
            {
                //-1 means complete
                dm.CurrentStep = -1;
                dm.Status = SupportTypeStatus.ShenHeWanCheng;
            }
        }

        public void GoToPreviousStep(FuKuanDanModel dm)
        {
            int potentialStepNumber = dm.CurrentStep - 1;

            //step number is zero based
            if (potentialStepNumber >= 0)
                dm.CurrentStep = potentialStepNumber;

            if (dm.CurrentStep == 0)
                dm.Status = SupportTypeStatus.CaoGao;
        }

        private void UpdateViewDataAtRuntime(FuKuanDanModel dm)
        {
            //can operate
            var allStepsForCurrentUser = QuanXianAndProcessHelper.GetUserStepNumber(SupportTypes.FukuanDan, AccountHelper.GetCurrentUser(this));
            bool canOperate = allStepsForCurrentUser.Contains(dm.CurrentStep);
            ViewData["canOperateFuKuanDan"] = canOperate;

            //是否是填表员
            ViewData["isTianbiaoyuan"] = false;
            var userDb = AccountHelper.GetCurrentUser(this);
            if (userDb.idUser == dm.CreateUser_idUser)
                ViewData["isTianbiaoyuan"] = true;
        }
    }
}
